//
//  XQLoadingView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/1/28.
//

import UIKit

class XQLoadingView: UIView {
    
    init(title: String? = nil,
         config : XQLoadingConfig) {
 
        super.init(frame: CGRect.zero)
        
        let effectView = UIVisualEffectView(effect: UIBlurEffect(style: config.effectStyle))
        addSubview(effectView)
        effectView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        effectView.isHidden = true
        backgroundColor = config.bgColor
        
        let containerView = UIView.init()
        addSubview(containerView)
        
        containerView.backgroundColor = config.tintColor
        containerView.layer.cornerRadius = config.cornerRadius
        
        containerView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }

        let loadingView = XQCommonView(message : title,
                              image: UIImage(),
                              imageType: config.imageType,
                              spaceImage: config.spaceImage,
                              spaceText: config.spaceText)
        
        loadingView.messageLb.textColor = config.messageColor
        loadingView.messageLb.font = config.messageFont
        
        containerView.addSubview(loadingView)

        loadingView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(config.verticalPadding)
            make.bottom.equalToSuperview().offset(-config.verticalPadding)
            make.left.equalToSuperview().offset(config.horizontalPadding)
            make.right.equalToSuperview().offset(-config.horizontalPadding)
            make.width.lessThanOrEqualTo(config.maxWidth)
        }
        
        let activityIndicatorView = UIActivityIndicatorView.init(style: .large)
        activityIndicatorView.color = config.activityColor
        let transform = CGAffineTransform.init(scaleX: 1.2, y: 1.2)
        activityIndicatorView.transform = transform
        activityIndicatorView.startAnimating()
        loadingView.imageView.addSubview(activityIndicatorView)
        activityIndicatorView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   

}
